#!/usr/bin/python

from tools_core.stm32_rw import lowlevel_rw_fun
import struct
import os
import sys

r_value = []
w_value = []

fpga_selete = 1
fpga_rw = input("please input read or write r=1 or w=2:")
fpga_selete = input("please input fpga id a=1 or b=2:")
fpga_reg = input('please input fpga reg:')
if(fpga_rw == 1):
    r_len = input('please input read len:')
else:
    w_value = input('please input what you will write:(example:0x01,0x02,0x03,0x04)\n')
    print(len(w_value))

if(fpga_rw == 1):
    tag = (0x200f0003|0x80000000)
else:
    tag = 0x200f0003

value = [0,0,0,0,0,0,0,0,0]


if(fpga_selete == 1):
    value[0] = 1                 #id
else:
    value[0] = 2                 #id
value[1] = fpga_reg&0xFF         #reg
value[2] = (fpga_reg>>8)&0xFF
value[3] = (fpga_reg>>16)&0xFF
value[4] = (fpga_reg>>24)&0xFF
value[5] = 0                     #offset
value[6] = 0
if(fpga_rw == 1):
    value[7] = (r_len)&0xFF          #len
    value[8] = (r_len>>8)&0xFF
else:
    for i in range(len(w_value)):
        value[7+i] = w_value[i]

ret_value = []

ret_value = lowlevel_rw_fun(tag,value)
print(ret_value)

if(fpga_selete == 1):
    print('fpga a read/write successfully!')
else:
    print('fpga b read/write successfully!')
